﻿using System;
using System.Windows.Forms;
using DigitalRune.Windows.Docking;
using md.imi.membrane.common.Interfaces;
using md.imi.membrane.common.Messaging;
using md.imi.membranes.objects;
using md.imi.membranes.objects.strings;

namespace md.imi.membranes.gui.Info
{
    public partial class GeneratedLanguageView : DockableForm, IMessageViewer
    {
        public object syncObject = new object();

        //  defaultConstructor
        public GeneratedLanguageView()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     Add a information entity to this view. Only string objects 
        /// formed from Terminal Alphabet objects are added to this view
        /// Check IsTerminal of eachalphabet object, before adding
        /// </summary>
        /// <param name="message"></param>
        public void AddMessage(membrane.common.Messaging.MessageData message)
        {
            if ((message == null) || (message.Data == null))
            {
                return;
            }
            AddMessageSafe(message);
        }


        /// <summary>
        ///     Perform BeginInvoke method calling
        /// </summary>
        /// <param name="message"></param>
        private void AddMessageSafe(MessageData message)
        {
            // add only terminal strings
            //var so = message.Data as StringObject;
            // if (so == null || so.IsTerminal() == false)
            //{
            //    return;
            //}

            if (this.InvokeRequired)
            {
               this.BeginInvoke( new MethodInvoker(() => AddMesageUnsafe(message)), null);
            }
            else
            {
                AddMesageUnsafe(message);

            }
        }

        private void AddMesageUnsafe(MessageData message)
        {
            var so = message.Data as StringObject;
            if (so == null || so.IsTerminal() == false)
            {
                return;
            }

            var lvItem = lvGenLanguaes.Items.Add(so.ToString());
            lvItem.Tag = message;
            lvItem.SubItems.Add(message.Source.ToString());
        }


        public void ClearView()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(() => lvGenLanguaes.Clear()), null);
            }
            else
            {
                lvGenLanguaes.Clear();

            }
            
        }

        /// <summary>
        ///     Object specific destination type
        /// </summary>
        /// <returns></returns>
        public DestinationType GetDestinationType()
        {
            return DestinationType.GeneratedLanguageTarget;
        }
    }
}